package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品实现类
 */
@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IProductMapper productMapper;
    @Override
    public IPage<ProductBean> cutProductByItem(Integer pageNO, String productName, String classes, Integer startNum, Integer endNum, String sellPrice) {
        QueryWrapper<ProductBean> qw=new QueryWrapper<>();
        if (productName !=null && productName.length() !=0){
            qw.like("p_name",productName);
        }

        if (classes !=null){
            qw.eq("p_classes",classes);
        }
        if (startNum !=null){
            qw.ge("p_stock",startNum);
        }
        if (endNum !=null){
            qw.le("p_stock",endNum);
        }
        if (sellPrice.equals("未定销售价")){
            qw.isNull("p_sellPrice");
        }
        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public IPage<ProductBean> cutProductPriceByItem(Integer pageNO, String productName, String classes) {
       QueryWrapper<ProductBean> qw=new QueryWrapper<>();
        if (productName !=null && productName.length() !=0){
            qw.like("p_name",productName);
        }

        if (classes !=null){
            qw.eq("p_classes",classes);
        }
        qw.isNotNull("p_sellPrice");
        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public ProductBean findProductById(Integer productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public void updateSellPrice(Integer productId, Integer newSellPrice) {
            ProductBean productObj=productMapper.selectById(productId);
            productObj.setSellPrice(newSellPrice);
            productMapper.updateById(productObj);
    }
}
